home *** CD-ROM | disk | FTP | other *** search
- Path: soap.news.pipex.net!pipex!usenet
- From: m.hendry@dial.pipex.com (Mathew Hendry)
- Newsgroups: comp.sys.amiga.programmer
- Subject: Re: Help with an easy C issue.
- Date: Sat, 13 Jan 96 04:06:18
- Organization: Private node.
- Distribution: world
- Message-ID: <19960113.44C1D0.3D32@ak056.du.pipex.com>
- References: <737.6585T1284T2078@in.net>
- NNTP-Posting-Host: ak056.du.pipex.com
- X-Newsreader: TIN [AMIGA 1.3 950726BETA PL0]
-
- John J. Maver, Jr. (mave@in.net) wrote:
- : I have a value entered as a string like,
- :
- : char *mystring;
- :
- : printf("Enter your age>");
- : scanf("%s",&mystring);
- :
- : Now I want it to be an int, so:
- :
- : myint=atoi(mystring);
- :
- : I mess around with the int:
- :
- : myint=myint+10;
- :
- : And I want to turn myint back into a string.
- :
- : HOW???
-
- Hey, even I can answer this ;)
-
- The following is an example from Kernighan and Ritchie.
-
- /* itoa: convert n to characters in s */
- void itoa(int n, char s[])
- {
- int i, sign;
-
- if ((sign = n) < 0)
- n = -n;
- i = 0;
- do {
- s[i++] = n % 10 + '0';
- } while (( n /= 10) > 0);
- if (sign < 0)
- s[i++] = '-';
- s[i] = '\0';
- reverse(s);
- }
-
- Not the fastest way of going about things, and there may be something similar
- functions in your compiler's libraries, but there it is anyway.
-
- -- Mat.
-